package com.example.su_shi_tan_core.db

import com.alibaba.fastjson.JSON
import com.example.su_shi_tan_core.bean.SignUserBean
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream

object UserInfoBeanHolder {

   private var mUserInfoBean: UserInfoBean? = null

    fun onSave(json: String) {
        mUserInfoBean = JSON.parseObject(json, UserInfoBean::class.java)
        val userInfoBeanWrapper = UserInfoBeanWrapper()
        userInfoBeanWrapper.token = LogInTokenManager.tokenManager.getLogInToken() ?: ""
        if (mUserInfoBean != null) {
            userInfoBeanWrapper.data = toByteArray(mUserInfoBean)
        }

        UserInfoDatabase.getInstance()
            .getUserInfoDao()
            .save(userInfoBeanWrapper)

    }

    fun onSave(mSignUserBean: SignUserBean?){
        mUserInfoBean?.mSignUserBean=mSignUserBean
        val userInfoBeanWrapper = UserInfoBeanWrapper()
        userInfoBeanWrapper.token = LogInTokenManager.tokenManager.getLogInToken() ?: ""
        if (mUserInfoBean != null) {
            userInfoBeanWrapper.data = toByteArray(mUserInfoBean)
        }

        UserInfoDatabase.getInstance()
            .getUserInfoDao()
            .save(userInfoBeanWrapper)
    }

    fun onDelete(){

        val userInfoBeanWrapper = UserInfoBeanWrapper()
        userInfoBeanWrapper.token = LogInTokenManager.tokenManager.getLogInToken() ?: ""
        if (mUserInfoBean!=null){
            userInfoBeanWrapper.data= toByteArray(mUserInfoBean)
        }
        UserInfoDatabase.getInstance()
            .getUserInfoDao()
            .delete(userInfoBeanWrapper)
    }

    private fun onUpDate(): UserInfoBean? {
        var mUserInfoBean: UserInfoBean? = null
        val mToken = LogInTokenManager.tokenManager.getLogInToken() ?: ""
        val userInfoBeanWrapper = UserInfoDatabase.getInstance()
            .getUserInfoDao().getCache(mToken)
        val data = userInfoBeanWrapper?.data
        if (data != null) {
            val toObject = toObject(data)
            if (toObject!=null){
                mUserInfoBean = toObject as UserInfoBean
            }

        }
        return mUserInfoBean
    }

    fun getUserInfoBean(): UserInfoBean? {
        if (mUserInfoBean == null) {
            mUserInfoBean = onUpDate()
        }
        return mUserInfoBean
    }


    //反序列,把二进制数据转换成java object对象
    private fun toObject(data: ByteArray): Any? {
        var bais: ByteArrayInputStream? = null
        var ois: ObjectInputStream? = null
        try {
            bais = ByteArrayInputStream(data)
            ois = ObjectInputStream(bais)
            return ois.readObject()
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        } finally {
            try {
                bais?.close()
                ois?.close()
            } catch (ignore: java.lang.Exception) {
                ignore.printStackTrace()
            }
        }
        return null
    }

    //序列化存储数据需要转换成二进制
    private fun <T> toByteArray(body: T): ByteArray? {
        var baos: ByteArrayOutputStream? = null
        var oos: ObjectOutputStream? = null
        try {
            baos = ByteArrayOutputStream()
            oos = ObjectOutputStream(baos)
            oos.writeObject(body)
            oos.flush()
            return baos.toByteArray()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            try {
                baos?.close()
                oos?.close()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return ByteArray(0)
    }
}